package com.kotlin.flong.flow

import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
import java.lang.RuntimeException

/**
 * @Author:liangjl
 * @Date: 2020/7/11-21:17
 * @Eamil:jilongliang@sina.com
 * @Description:
 * onCompletion中间操作的一个优势在于它可空的Throwable参数，可用作确定Flow的收集操作是正常完成还是异常完成的
 *
 */
private fun myMethod(): Flow<Int> = flow {
    emit(1)
    throw RuntimeException()
}

fun main() = runBlocking<Unit> {
    myMethod().onCompletion { cause ->
        if (cause != null) {
            println("Flow completion Exception")
        }
    }.catch { cause ->
        println("catch entered ")
    }.collect { println(it) }

}